#include "machinemodel.h"
#include <QSettings>
#include <QProcess>
#include <QDebug>

std::shared_ptr<MachineModel> MachineModel::m_machineModel = nullptr;
std::mutex MachineModel::m_mutex;

MachineModel::MachineModel(QObject *parent) : QObject(parent)
{
    initMachineType();
}

QString MachineModel::getTheMachineType()
{
    return m_machineType;
}

std::shared_ptr<MachineModel> MachineModel::getMachineModelInstance()
{
    if(m_machineModel == nullptr)
    {
        std::unique_lock<std::mutex> lock(m_mutex);
        if(m_machineModel == nullptr)
        {
            m_machineModel = std::shared_ptr<MachineModel>(new MachineModel);
        }
    }
    return m_machineModel;
}

void MachineModel::initMachineType()
{
    QSettings setting(":/conf.ini", QSettings::IniFormat);
    setting.beginGroup("MachineType");//节点开始

    QString type = getSysVendor() + getProductFamily();
    qInfo() << __FILE__ << __LINE__ << type;
    if (setting.contains(type)) {
        m_machineType = setting.value(type).toString();
    } else {
        m_machineType = QString();
    }
    qInfo() << __FILE__ << __LINE__ << m_machineType;
    setting.endGroup();//节点结束

}

const QString MachineModel::getSysVendor() const
{
    QProcess process;
    QStringList options;
    options << "-c";
    options << "cat /sys/class/dmi/id/sys_vendor";
    process.start("/bin/bash", options);
    process.waitForFinished();
    QString result = process.readAllStandardOutput();

    QStringList list = result.split("\n");
    result = list.at(0);
    qInfo() << __FILE__ << __LINE__ << "获取设备厂商为：" << result;
    return result;
}

const QString MachineModel::getProductName() const
{
    QProcess process;
    QStringList options;
    options << "-c";
    options << "cat /sys/class/dmi/id/product_name";
    process.start("/bin/bash", options);
    process.waitForFinished();
    QString result = process.readAllStandardOutput();

    QStringList list = result.split("\n");
    result = list.at(0);
    qInfo() << __FILE__ << __LINE__ << "获取产品名为：" << result;
    return result;
}

const QString MachineModel::getProductFamily() const
{
    QProcess process;
    QStringList options;
    options << "-c";
    options << "cat /sys/class/dmi/id/product_family";
    process.start("/bin/bash", options);
    process.waitForFinished();
    QString result = process.readAllStandardOutput();

    QStringList list = result.split("\n");
    result = list.at(0);
    qInfo() << __FILE__ << __LINE__ << "获取设备Family为：" << result;
    return result;
}
